package com.lej.core.crash

import android.app.AlertDialog
import android.content.ClipData
import android.content.ClipboardManager
import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.lej.core.R

class DefaultErrorActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //This is needed to avoid a crash if the developer has not specified
        //an app-level theme that extends Theme.AppCompat
        val a = obtainStyledAttributes(R.styleable.AppCompatTheme)
        if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
            setTheme(R.style.Theme_AppCompat_Light_DarkActionBar)
        }
        a.recycle()
        setContentView(R.layout.crash_default_error_activity)

        //Close/restart button logic:
        //If a class if set, use restart.
        //Else, use close and just finish the app.
        //It is recommended that you follow this logic if implementing a custom error activity.
        val restartButton = findViewById<Button>(R.id.restart_button)
        val config = CustomActivityOnCrash.getConfigFromIntent(intent)
        if (config.isShowRestartButton && config.restartActivityClass != null) {
            restartButton.setText(R.string.crash_error_restart_app)
            restartButton.setOnClickListener { v: View? ->
                CustomActivityOnCrash.restartApplication(
                    this@DefaultErrorActivity,
                    config
                )
            }
        } else {
            restartButton.setOnClickListener {
                CustomActivityOnCrash.closeApplication(
                    this@DefaultErrorActivity,
                    config
                )
            }
        }
        val moreInfoButton = findViewById<Button>(R.id.more_info_button)
        if (config.isShowErrorDetails) {
            moreInfoButton.setOnClickListener {
                //We retrieve all the error data and show it
                val dialog = AlertDialog.Builder(this@DefaultErrorActivity)
                    .setTitle(R.string.crash_error_details_title)
                    .setMessage(
                        CustomActivityOnCrash.getAllErrorDetailsFromIntent(
                            this@DefaultErrorActivity,
                            intent
                        )
                    )
                    .setPositiveButton(R.string.crash_error_details_close, null)
                    .setNeutralButton(
                        R.string.crash_error_details_copy
                    ) { _: DialogInterface?, _: Int ->
                        copyErrorToClipboard()
                        Toast.makeText(
                            this@DefaultErrorActivity,
                            R.string.crash_error_details_copied,
                            Toast.LENGTH_SHORT
                        ).show()
                    }
                    .show()
                dialog.findViewById<TextView>(android.R.id.message).textSize = 12f
            }
        } else {
            moreInfoButton.visibility = View.GONE
        }
    }

    private fun copyErrorToClipboard() {
        val errorInformation =
            CustomActivityOnCrash.getAllErrorDetailsFromIntent(this@DefaultErrorActivity, intent)
        val clip = ClipData.newPlainText(
            getString(R.string.crash_error_details_clipboard_label),
            errorInformation
        )
        (getSystemService(CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(clip)
    }
}